//
//  UserGridCell.m
//  FootPrint
//
//  Created by alex tao on 3/21/13.
//  Copyright (c) 2013 alex tao. All rights reserved.
//

#import "UserGridCell.h"

@implementation UserGridCell

- (void)internalInit
{
    [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
    
    _contentView.frame = self.bounds;
    [self addSubview:_contentView];
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        // Initialization code
        [self internalInit];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self internalInit];
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)dealloc {
    [_contentView release];
    [_userIcon release];
    [_userNameLabel release];
    [_statImage release];
    [super dealloc];
}

- (void) updateUserState:(EventMessage_UserStatus*)stat
{
    _userNameLabel.text = stat.user.nickName;
    
    if (!stat.user.portrait.isSys) {
        [_userIcon loadAsyncImage:stat.user.portrait.imageUrl];
    } else {
        [_userIcon setSysImageWithId:stat.user.portrait.id];
    }
    
    if (stat.status == eStatAccept) {
        _statImage.image = [UIImage imageNamed:@"state_received.png"];
    } else if (stat.status == eStatWait) {
        _statImage.image = [UIImage imageNamed:@"state_wait.png"];
    } else {
        _statImage.image = [UIImage imageNamed:@"state_refused.png"];
    }
}

- (void) updateUserMessage:(UserMessage*)stat
{
    _userNameLabel.text = stat.nickName;
    
    if (!stat.portrait.isSys) {
        [_userIcon loadAsyncImage:stat.portrait.imageUrl];
    } else {
        [_userIcon setSysImageWithId:stat.portrait.id];
    }
    
    _statImage.image = [UIImage imageNamed:@"state_wait.png"];
}

- (void) updateEventMessage:(EventMessage*)stat
{
    _userNameLabel.text = stat.title;
    
    if (!stat.image.isSys) {
        if ([[ImageManager sharedInstance] imageExistForId:stat.image.id]) {
            [_userIcon setLocalImageName:[[ImageManager sharedInstance] imageFileForImageId:stat.image.id]];
        } else {
            [_userIcon loadAsyncImage:stat.image.imageUrl];
        }
    } else {
        [_userIcon setSysImageWithId:stat.image.id];
    }
    
    _statImage.image = nil;
}

- (void) updateMerchantMessage:(MerchantMessage*)stat
{
    _userNameLabel.text = stat.name;
    
    if (!stat.logo.isSys) {
        if ([[ImageManager sharedInstance] imageExistForId:stat.logo.id]) {
            [_userIcon setLocalImageName:[[ImageManager sharedInstance] imageFileForImageId:stat.logo.id]];
        } else {
            [_userIcon loadAsyncImage:stat.logo.imageUrl];
        }
    } else {
        [_userIcon setSysImageWithId:stat.logo.id];
    }
    
    _statImage.image = nil;
}

@end
